package com.note.simpleadmin.module.shop.controller;
import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.dto.OrderDto;
import com.note.simpleadmin.entity.TbOrder;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.dao.OrderRepository;
import com.note.simpleadmin.module.shop.service.OrderService;
import com.note.simpleadmin.module.shop.vo.GoodsVo;
import com.note.simpleadmin.vo.Msg;
import com.note.simpleadmin.vo.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/***
 *@ClaseName:OrderController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/2/18 0018 0:04
 */
@Controller
@RequestMapping(value = "shop/order")
public class OrderController {
    private final Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;
    @RequestMapping(value = "index")
    public String index() {
        return "shop/goods/order";
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(OrderDto dto) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            PageBean<TbOrder> page =orderService.findByPage(dto);
            msg.setData(page);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "detail")
    public String detail() {
        return "shop/goods/orderDetail";
    }
    @RequestMapping(value = "ajax_detail")
    @ResponseBody
    public Msg ajax_detail(String id) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            TbOrder tbOrder=orderService.getOne(id);
            msg.setData(tbOrder);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "writeWaybillN")
    @ResponseBody
    public Msg writeWaybillN(String id,String logisticsCode,String logisticsNo) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            orderService.writeWaybillN(id,logisticsCode,logisticsNo);
            msg.setMsg("订单号已填写");
        }
        catch(DataNotExistException e) {
            logger.info(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
}